#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
string op;
int a, b;
int n, m;
int par[maxn];
int cnt[maxn];
void init() {
  for(int i=0; i<=n; ++i) par[i] = i;
  for(int i=0; i<=n; ++i) cnt[i] = 1;
}
int find(int x) {
  return par[x] = (par[x]==x? x:find(par[x]));
}
void unite(int u, int v) {
  u = find(u), v = find(v);
  if(u != v) {
    par[v] = u;
    cnt[u] += cnt[v];
    cnt[v] = cnt[u];
  }
  else {
    return;
  }
}
bool same(int a, int b) {
  return find(a) == find(b);
}
int query(int x) {
  return cnt[x];
}
int main() {
  cin>>n>>m;
  init();
  while(m--) {
    cin>>op;
    if("C"==op) {
      cin>>a>>b;
      unite(a, b);
    }
    else if("Q1" == op) {
      cin>>a>>b;
      cout<<(same(a, b)?"Yes\n":"No\n");
    }
    else if("Q2" == op) {
      cin>>a;
      cout<<cnt[find(a)]<<'\n';
    }
  }
}
